function init(){
    addimg.src = picsrc1;
    addimghover.src = picsrc2;
    findimg.src = findpicsrc1;
    findimghover.src = findpicsrc2;
    shareimg.src = sharepicsrc1;
    shareimghover.src = sharepicsrc2;
    fastimg.src = fastpicsrc1;
    fastimghover.src = fastpicsrc2;

    

    addimgBtn.showTip("新建笔记");
    findimgBtn.showTip("查找笔记");
    shareimgBtn.showTip("分享笔记");
    fastimgBtn.showTip("快捷方式")
    
    s1.innerHTML = currentUser.tel;
}
//1.初始化页面，给页面的层或图片赋值
init();

var isLoad = false;
var isover = false;
var p = 0;

function loadData(){
    if(!isLoad && !isover){
        p++;
        console.log(p);
    loading2.style.display  = "block";
    isLoad = true
    ajax("/biji/all2",{"page":p,"size":10,"sortNum":1},
    function(data){
        //d2.innerHTML = "";
         setTimeout(function(){
            var totalPages = data.totalPages;
            data = data.content;

            if (p == totalPages){
                isover = true;
                d3.innerHTML = "划不动了，别划了！"
            }

            for(let i=0;i<data.length;i++){
                //创建笔记层
                var newDiv = document.createElement("div");
                newDiv.onclick = function(){
                    newDivClick(data[i]);
                };
                newDiv.className = "biji clearfix";
               // 创建左侧层
                var leftDiv = document.createElement("div");
                leftDiv.className = "left fl";

                // 创建标题层，并解决长度过长的问题

                var titleDiv = document.createElement("div");
                titleDiv.className = "biji_title";
                var titleTxt = data[i].title;
                if(titleTxt){
                    titleDiv.innerHTML = titleTxt.length < 15 ? titleTxt : titleTxt.substring(0,15) + "...";
                    leftDiv.appendChild(titleDiv);
                }

                // 创建标题层结束

            // 创建时间层，并解决时间显示问题
                var dateDiv = document.createElement("div");
                dateDiv.className = "biji_date";
                var createdate = data[i].createDate;
                if(createdate){
                    var arr = createdate.split(/[\/: ]/);
                    if(arr.length==6){
                        var time1 = new Date(arr[0],arr[1]-1,arr[2],arr[3].substring(2),arr[4],arr[5]).getTime()
                        var time2 = new Date().getTime();
                        var diff = time2-time1;

                        if(diff<0){
                            dateDiv.innerHTML = "时空传送";
                        }else if(diff<5*1000){
                            dateDiv.innerHTML = "刚刚";
                        }else if(diff<60*1000){
                            dateDiv.innerHTML = Math.floor(diff/1000)+"秒前";
                        }else if(diff<60*60*1000){
                            dateDiv.innerHTML = Math.floor(diff/(1000*60))+"分钟前";
                        }else if(diff<60*60*1000*24){
                            dateDiv.innerHTML = Math.floor(diff/(1000*60*24))+"小时前";
                        }else if(diff<60*60*1000*24*30){
                            dateDiv.innerHTML = Math.floor(diff/(1000*60*24*30))+"天前";
                        }else if(diff<60*60*1000*24*30*365){
                            dateDiv.innerHTML = Math.floor(diff/(1000*60*60*24*30))+" 月前";
                        }else if(diff<60*60*1000*24*30*365*100){
                            dateDiv.innerHTML = Math.floor(diff/(1000*60*60*24*30*365))+" 年前";
                        }
                        leftDiv.appendChild(dateDiv);
                    }
                }

                var contentDiv = document.createElement("div");
                contentDiv.className = "biji_content";
                var cnt = data[i].content;
                var txt = data[i].contextTxt;

                if(!txt){
                    txt = cnt;
                }
                contentDiv.innerHTML = txt.length<30?txt:txt.substring(0,30)+"...";
                leftDiv.appendChild(contentDiv);

                newDiv.appendChild(leftDiv);

                var rightDiv = document.createElement("div");
                rightDiv.className = "right fl";

                if(cnt.indexOf("<img")!=-1){
                    var start = cnt.indexOf("<img");
                    var end = cnt.indexOf(">",start);
                    var imgHTML = cnt.substring(start,end+1);
                    var start2 = imgHTML.indexOf("src=\"");
                    var end2 = imgHTML.indexOf("\"",start2+5);
                    var picsrc = imgHTML.substring(start2+5,end2);
                    rightDiv.style.background = "url("+picsrc+") no-repeat center center";
                    rightDiv.style.backgroundSize = "cover";
                    newDiv.appendChild(rightDiv);
                }else{
                    leftDiv.style.width = "100%"
                }

                var deleteEle = document.createElement("button");
                deleteEle.innerHTML = "删除";
                deleteEle.style.display = "block";
                deleteEle.style.clear = "both";

                deleteEle.onclick = function(){
                  deleteBiJi(data[i].id);
                };
                newDiv.appendChild(deleteEle);

                d2.appendChild(newDiv);

                
            }
            if(document.getElementById("loading2")){
                loading2.style.display  = "none";
            }
            isLoad = false;

        },100);
    })
}
}

//2。加载所有笔记数据
loadData(p);

//当页面滚动时
//防抖和节流
window.onscroll = function(){
    var sh = document.documentElement.scrollHeight;
    var ch = document.documentElement.clientHeight;
    var st = document.documentElement.scrollTop||
             document.body.scrollTop;

    if (sh < st + ch + 100){
        this.loadData();
    }
}

//3.增加笔记按钮
addimgBtn.onclick = function(){
    d1.style.display = "none";
    addDiv.style.display = "block";
    addOrUpdate.innerHTML = "新增";
    title.value = "";
    content.innerHTML = "";
    title,focus();
}
//4.保存笔记按钮
finishBtn.onclick = function(){
    loading.style.display = "block";
    var titleValue = title.value;
    var contentValue = content.innerHTML;
    var contextTxt = content.innerText.trim();

    if(titleValue.length==0||contentValue==0){
        alert("请填写内容");
        title.focus();
        return;
    }

    ajax("/biji/add",{
        "biJiUser":
        {"id":currentUser.id
    },
        "content":contentValue,
        "createDate":new Date().toLocaleString(),
        "title":titleValue,
        "contextTxt":contextTxt

    },function(data){
        if(data){
            d1.style.display = "block";
            addDiv.style.display = "none";
            loading.style.display = "none";
            title.value = "";
            content.innerHTML = "";
            location.reload();
        }
    }
    );
}
//5.返回按钮
returnBtn.onclick = function(){
        d1.style.display = "block";
        addDiv.style.display = "none";
        document.documentElement.scrollTop = st;
    }


//7.删除笔记
function deleteBiJi(id){
    if(confirm("确定要删除这个笔记？")){
        ajax("/biji/del",{id:id},function(data){
            if(data){
                loadData();
            }else{
                alert("删除失败！")
            }
        });
    }
}

var st;
//8具体内容显示和编辑
function newDivClick(data){
    st = document.documentElement.scrollTop;
    console.log(data);
    d1.style.display = "none";
    addDiv.style.display = "block";
    title.value = data.title;
    content.innerHTML = data.content;
    document.documentElement.scrollTop = 0;
    addOrUpdate.innerHTML = "编辑";
}